package game;


import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.LinkedList;

public class Ainteligence 
{
	AnimatedSprite enemie;
	GameObject  personagem;
	GameObject  personagemSave;
	
	
	Point2D myPos = new Point2D.Double(0, 0);
	

	static LinkedList<Point2D> lRect = new LinkedList<Point2D>();
	
	Rectangle[] rectMap;
	
	public LinkedList<Point2D> path = new LinkedList<Point2D>();
	Point2D moveDirection, tempPos;
    boolean getDirection = true, beforePoint = false;
    Pathfinder pathfinder;
    Point endPoint;
    
    private static int tileSize = 40;
	
	
	public Ainteligence(AnimatedSprite enemie, GameObject personagem, Rectangle[] rectMapa, float velocity)
	{
		this.enemie = enemie;
		this.personagem = personagemSave = personagem;
		rectMap = rectMapa;
		
		
		
		
		this.enemie.position = this.myPos = this.PosicaoInicial();
		
		//this.myPos = x;
		
		pathfinder = new Pathfinder();
		
		
		Point2D p2 = new Point2D.Double(
				Player.pos.getX() - BaseMapa.mapPos.getX(),
				Player.pos.getY() - BaseMapa.mapPos.getY()
				);
		
		endPoint = new Point(
				(int)((p2.getX() - (p2.getX()%tileSize)) / tileSize),
				(int)((p2.getY() - (p2.getY()%tileSize)) / tileSize)
				);
		
		path = pathfinder.FindPath(new Point((int)(myPos.getX() / tileSize),
											 (int)(myPos.getY() / tileSize)),
								   endPoint);
	}
	
	public void Update()
	{	
	
		if (beforePoint == true)
        {
            beforePoint = false;
            path = pathfinder.FindPath(new Point((int)(myPos.getX() / tileSize), (int)(myPos.getY() / tileSize)), new Point(0, 1));
        }

        if (path != null && path.size() > 0)
        {
            if (getDirection)
            {
                getDirection = false;
                moveDirection = new Point2D.Double((path.get(0).getX() / tileSize) - (myPos.getX() / tileSize), (path.get(0).getY() / tileSize) - (myPos.getY() / tileSize));
                tempPos = myPos;
            }
            myPos = new Point2D.Double(myPos.getX() + (moveDirection.getX() * 2), myPos.getY() + (moveDirection.getY() * 2));
            myPos = new Point2D.Double(Math.max(tempPos.getX() - tileSize, Math.min(tempPos.getX() + tileSize, myPos.getX())), Math.max(tempPos.getY() - tileSize, Math.min(tempPos.getY() + tileSize, myPos.getY())));
            
            if (path.get(0).getX() == myPos.getX() && path.get(0).getY() == myPos.getY())
            {
                path.remove(path.get(0));
                endPoint = new Point((int)((Player.pos.getX() - BaseMapa.mapPos.getX()) / tileSize), (int)((Player.pos.getY() - BaseMapa.mapPos.getY()) / tileSize));
                try 
                {
                	if(pathfinder.FindPath(new Point((int)(myPos.getX() / tileSize), (int)(myPos.getY() / tileSize)), endPoint).size() == 0 &&
	                    new Point((int)myPos.getX() / tileSize, (int)myPos.getY() / tileSize) != endPoint)
	                    beforePoint = true;
                	else
                        path = pathfinder.FindPath(new Point((int)(myPos.getX() / tileSize), (int)(myPos.getY() / tileSize)), endPoint);
                    getDirection = true;
                }
                catch (Exception e)
                {
                }
                
            }
        }
        enemie.position = new Point2D.Double(myPos.getX() + BaseMapa.mapPos.getX(), myPos.getY() + BaseMapa.mapPos.getY());
        
        try{
        	this.AtualizarAnimacao();
        }
        catch(Exception e){}
		
	}

	
	public static void CriarCasasEnemie()
	{
		lRect.clear();
		for (int i = 0; i < Map.GetInstance().Width(); i++) 
		{
			if(Map.GetInstance().GetIndex(i, 0) == 0)
				lRect.add(new Point2D.Double(i*tileSize, 0));
			
			if(Map.GetInstance().GetIndex(i, Map.GetInstance().Height()-1) == 0)
				lRect.add(new Point2D.Double(i*tileSize, (Map.GetInstance().Height()-1)*tileSize));
		}
		
		for (int i = 0; i < Map.GetInstance().Height(); i++) 
		{
			if(Map.GetInstance().GetIndex(0, i) == 0)
				lRect.add(new Point2D.Double(0, i*tileSize));
			
			if(Map.GetInstance().GetIndex(Map.GetInstance().Width()-1, i) == 0)
				lRect.add(new Point2D.Double((Map.GetInstance().Width()-1)*tileSize, i*tileSize));
		}
	}
	
	public Point2D PosicaoInicial()
	{
			Point2D x = lRect.get(Game.rand.nextInt(lRect.size())); 
			
			Point2D x1 = new Point2D.Double(
					x.getX() + BaseMapa.mapPos.getX() - (BaseMapa.mapPos.getX()%40),
					x.getY() + BaseMapa.mapPos.getY() - (BaseMapa.mapPos.getY()%40)
					);
			if(x1.getX() < Game.screenWidth - BaseMapa.sizeMap.getX())
			{
				x1 = new Point2D.Double(
						x.getX() + BaseMapa.mapPos.getX() + (40-(BaseMapa.mapPos.getX()%40)),
						x.getY()
						);
			}
			
			if(x1.getY() < Game.screenHeigth - BaseMapa.sizeMap.getY())
			{
				x1 = new Point2D.Double(
						x.getX(),
						x.getY() + BaseMapa.mapPos.getY() + (40-(BaseMapa.mapPos.getY()%40))
						);
			}
			
			
			Rectangle r2 = new Rectangle(
					(int)(-BaseMapa.mapPos.getX()),
					(int)(-BaseMapa.mapPos.getY()),
					Game.screenWidth,
					Game.screenHeigth);
			
			
			if(r2.intersects(new Rectangle((int)x.getX(), (int)x.getY(), 40, 40)))
			{
				System.out.println("INIMIGO: TENTEI NASCER, MAS NASCI DENTRO DA TELA");
				return PosicaoInicial();
			}
			
			return x;
				
	}
	
	public void AtualizarAnimacao()
	{
		
		if(moveDirection.getX() == 1)
			enemie.animationType.y = 4;
		else if(moveDirection.getX() == -1)
			enemie.animationType.y = 2;
		else if(moveDirection.getY() == 1)
			enemie.animationType.y = 1;
		else if(moveDirection.getY() == -1)
			enemie.animationType.y = 3;
		
	}
	
	public AnimatedSprite returnEnemie()
	{
		return enemie;
	}


	

}
